package com.wing.web.aspect;

import com.wing.web.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Component
@Aspect
@Slf4j
public class ControllerAddUpdateAspect {

    @Pointcut("execution (* com.wing.*.controller..*.add*(..)) && !execution(* com.wing.user.controller.websocket..*.*(..))")
    private void addMethod(){

    }

    @Pointcut("execution (* com.wing.*.controller..*.update*(..)) && !execution(* com.wing.user.controller.websocket..*.*(..))")
    private void updateMethod(){

    }

    @Before("addMethod()")
    public void add(JoinPoint joinPoint){
        if (joinPoint.getArgs()==null||joinPoint.getArgs().length==0){
            return;
        }
        if (joinPoint.getArgs()[0] instanceof List||
                joinPoint.getArgs()[0] instanceof Map||
                joinPoint.getArgs()[0] instanceof Set ||
                joinPoint.getArgs()[0] instanceof Integer||
                joinPoint.getArgs()[0] instanceof String||
                joinPoint.getArgs()[0] instanceof Long||
                joinPoint.getArgs()[0] instanceof Boolean){
            return;
        }
        Object object=joinPoint.getArgs()[0];
        try{
            Method method=object.getClass().getMethod("setCreator",Long.class);
            if (method!=null){
                method.invoke(object,JwtUtils.getLoginUserId());
            }
            method=object.getClass().getMethod("setCreatorName",String.class);
            if (method!=null){
                method.invoke(object,JwtUtils.getLoginUsername());
            }
        }catch (Exception e){
            log.error("set field fail",e);
        }


    }


    @Before("updateMethod()")
    public void update(JoinPoint joinPoint){
        if (joinPoint.getArgs().length==0 || joinPoint.getArgs() == null){
            return;
        }
        if (joinPoint.getArgs()[0] instanceof List
                ||joinPoint.getArgs()[0] instanceof String
                ||joinPoint.getArgs()[0] instanceof Integer
                ||joinPoint.getArgs()[0] instanceof Byte
                ||joinPoint.getArgs()[0] instanceof Short
                ||joinPoint.getArgs()[0] instanceof Long
                ||joinPoint.getArgs()[0] instanceof Double
                ||joinPoint.getArgs()[0] instanceof Float
                ||joinPoint.getArgs()[0] instanceof Boolean)
        {
            return;
        }
        Object object=joinPoint.getArgs()[0];
        try{
            Method method=object.getClass().getMethod("setModifier",Long.class);
            if (method!=null){
                method.invoke(object,JwtUtils.getLoginUserId());
            }
            method=object.getClass().getMethod("setModifierName",String.class);
            if (method!=null){
                method.invoke(object,JwtUtils.getLoginUsername());
            }
        }catch (Exception e){
            log.error("set field fail",e);
        }

    }



}
